package com.yuanqunsoft.alimigration.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yuanqunsoft.alimigration.dao.IReportWithRulesetDao;
import com.yuanqunsoft.alimigration.model.ReportWithRuleset;

public class ReportWithRulesetDaoImpl  extends HibernateDaoSupport implements IReportWithRulesetDao{

    @Override
    public void save(ReportWithRuleset instance) {
        try{
            this.getHibernateTemplate().saveOrUpdate(instance);
        }catch(RuntimeException re){
            throw re;
        }
        
    }

    @Override
    public void delete(ReportWithRuleset instance) {
        try{
            this.getHibernateTemplate().delete(instance);
        }catch(RuntimeException re){
            throw re;
        }
       
    }

    @Override
    public void deleteByReportId(Integer repId) {
        try{
            List<ReportWithRuleset> repwrList =  this.findByReportId(repId);
            for(ReportWithRuleset r : repwrList){
                this.getHibernateTemplate().delete(r);
            }
        }catch(RuntimeException re){
            throw re;
        }
    }

    @Override
    public ReportWithRuleset findById(Integer repId) {
        try{
            ReportWithRuleset instance = (ReportWithRuleset) getHibernateTemplate().get(ReportWithRuleset.class, repId);
            return instance;
        }catch(RuntimeException re){
            throw re;
        }
    }

    @Override
    public List<ReportWithRuleset> findByReportId(Integer id) {
        try{
            String query = "from ReportWithRuleset r where r.report.repId=?";
            List<ReportWithRuleset> result = getHibernateTemplate().find(query, id);
            return result;
        }catch(RuntimeException re){
            throw re;
        }
    }

}
